package com.zmc.lostfound.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.lostfound.auth.dao.RolePermissionDao;
import com.zmc.lostfound.auth.entity.RolePermissionEntity;
import com.zmc.lostfound.auth.service.RolePermissionService;
import com.zmc.lostfound.auth.vo.RolePermissionSaveVO;
import com.zmc.lostfound.common.exception.MyException;
import com.zmc.lostfound.common.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author GXC
 * @description 角色&权限关联信息业务接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:17:37
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionDao, RolePermissionEntity> implements RolePermissionService {

    /**
     * 新增角色&权限关联信息
     *
     * @param rolePermissionSaveVO 角色&权限关联信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result insertRolePermission(RolePermissionSaveVO rolePermissionSaveVO) {
        RolePermissionEntity rolePermissionEntity = this.query()
                .eq("role_id", rolePermissionSaveVO.getRoleId())
                .eq("permission_id", rolePermissionSaveVO.getPermissionId())
                .one();
        if (rolePermissionEntity != null) {
            MyException.throwException("新增失败: 该角色&权限关联信息已存在，请勿重复添加");
        }
        rolePermissionEntity = BeanUtil.copyProperties(rolePermissionSaveVO, RolePermissionEntity.class);
        if (!this.save(rolePermissionEntity)) {
            MyException.throwException("新增角色&权限关联信息失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 修改角色&权限关联信息
     *
     * @param rolePermissionSaveVO 角色&权限关联信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result updateRolePermission(RolePermissionSaveVO rolePermissionSaveVO) {
        RolePermissionEntity rolePermissionEntity = this.query()
                .eq("role_id", rolePermissionSaveVO.getRoleId())
                .eq("permission_id", rolePermissionSaveVO.getPermissionId())
                .one();
        if (rolePermissionEntity != null) {
            MyException.throwException("修改失败: 该角色&权限关联信息已存在");
        }
        rolePermissionEntity = BeanUtil.copyProperties(rolePermissionSaveVO, RolePermissionEntity.class);
        if (!this.updateById(rolePermissionEntity)) {
            MyException.throwException("修改角色&权限关联信息失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 按主键ids批量删除角色&权限关联信息
     *
     * @param ids 主键ids
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result deleteBatchByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            MyException.throwException("删除失败: 主键ids为空");
        }
        this.removeByIds(ids);
        return Result.ok("删除成功");
    }

    /**
     * 按角色id和权限id删除角色&权限关联信息
     *
     * @param roleId       角色id
     * @param permissionId 权限id
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result deleteByRoleIdAndPermissionId(Long roleId, Long permissionId) {
        if (roleId == null && permissionId == null) {
            MyException.throwException("角色id和权限id不能同时为null");
        }
        this.remove(new QueryWrapper<RolePermissionEntity>()
                .eq(roleId != null, "role_id", roleId)
                .eq(permissionId != null, "permission_id", permissionId));
        return Result.ok("删除成功");
    }

}